package com.xuln.ipp.action;

import java.util.List;

import javax.annotation.Resource;

import com.xuln.framework.action.BaseAction;
import com.xuln.framework.entity.PrimaryKey;
import com.xuln.ipp.entity.Article;
import com.xuln.ipp.form.ArticleForm;
import com.xuln.ipp.service.IArticleService;

public class ArticleAction extends BaseAction {
	private static final long serialVersionUID = -6714115653589359271L;

	private ArticleForm articleForm;
	@Resource
	private IArticleService articleService;

	public String addArticle() throws Exception {
		Article article = articleForm.copy();
		article.setPK(new PrimaryKey(getUserLocale()));
		articleService.save(article);
		return getArticlesByCategory();
	}

	public String getArticle() throws Exception {
		Article article = articleService.getArticle(articleForm.getId(), getUserLocale());
		articleForm.setArticle(article);
		return "articleInfo";
	}

	public String getArticlesByCategory() throws Exception {
		List<Article> articles = articleService.getArticleByCatetoryId(articleForm.getCategoryId(), getUserLocale());
		articleForm.setArticles(articles);
		return "articleList";
	}

	public String editArticle() throws Exception {
		getArticle();
		return "articleEdit";
	}

	public String updateArticle() throws Exception {
		Article article = articleForm.copy();
		article.setPK(new PrimaryKey(articleForm.getId(), getUserLocale()));
		articleService.update(article);
		return getArticle();
	}

	public String delArticle() throws Exception {
		articleService.delete(articleForm.getId());
		return getArticlesByCategory();
	}

	public ArticleForm getArticleForm() {
		return articleForm;
	}

	public void setArticleForm(ArticleForm articleForm) {
		this.articleForm = articleForm;
	}

}
